/**
 *    Copyright 2011 M. Lauer
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
package com.googlecode.stew_faces.component;

import java.util.ArrayList;
import java.util.List;

import javax.faces.component.FacesComponent;
import javax.faces.component.UIComponent;
import javax.faces.component.UINamingContainer;

@FacesComponent("com.googlecode.stew_faces.component.EnhancedDataGridComponent")
public class EnhancedDataGridComponent extends UINamingContainer{
	
	
	enum PropertyKeys {
		value,allowDnD,allowNestedSorting
	};
	
	private List<DataGridColumnComponent> getChildColumns()
	{
		List<UIComponent> childs = getFacets().get("javax.faces.component.COMPOSITE_FACET_NAME").getChildren();
		return getChildColumns(childs);
	}
	
	private List<DataGridColumnComponent> getChildColumns(List<UIComponent> paramSearchList)
	{
		List<DataGridColumnComponent> resultList = new ArrayList<DataGridColumnComponent>();
		boolean childlevelColumn = false;
		for(UIComponent component:paramSearchList)
		{
			if(component instanceof DataGridColumnComponent)
			{
				resultList.add((DataGridColumnComponent) component);
				childlevelColumn=true;
			}
		}
		if(childlevelColumn)
			return resultList;
		if(!childlevelColumn)
		{
			for(UIComponent component:paramSearchList)
			{
				List<DataGridColumnComponent> childResult =  getChildColumns(component.getChildren());
				if(childResult!=null)
					return childResult;
			}
		}
		return null;
	}
	
	@SuppressWarnings("rawtypes")
	public List<?> getValue() {
		return (List<?>) getStateHelper().eval(PropertyKeys.value, new ArrayList());
	}

	public void setValue(List<?> paramValue) {
		getStateHelper().put(PropertyKeys.value, paramValue);
	}
	
	public Boolean getAllowDnD() {
		return (Boolean) getStateHelper().eval(PropertyKeys.allowDnD, false);
	}

	public void setAllowDnD(Boolean paramValue) {
		getStateHelper().put(PropertyKeys.allowDnD, paramValue);
	}	
	
	public Boolean getAllowNestedSorting() {
		return (Boolean) getStateHelper().eval(PropertyKeys.allowNestedSorting, false);
	}

	public void setAllowNestedSorting(Boolean paramValue) {
		getStateHelper().put(PropertyKeys.allowNestedSorting, paramValue);
	}	
	
	public String getLayout()
	{
		List<DataGridColumnComponent> columns = getChildColumns();
		String result = "{}";
		if(columns!=null&&columns.size()>0)
		{
			StringBuffer layoutStringBuffer = new StringBuffer();
			layoutStringBuffer.append("[");
			boolean firstTime = true;
			for(DataGridColumnComponent col:columns)
			{
				if(firstTime)
					firstTime=false;
				else
					layoutStringBuffer.append(",");
				layoutStringBuffer.append("{");
				layoutStringBuffer.append("field:'");
				layoutStringBuffer.append(col.getFieldId());
				layoutStringBuffer.append("',name:'");
				layoutStringBuffer.append(col.getTitle());
				layoutStringBuffer.append("',width:'");
				layoutStringBuffer.append(col.getWidth());
				layoutStringBuffer.append("px'");
				layoutStringBuffer.append("}");
			}
			layoutStringBuffer.append("]");
			return layoutStringBuffer.toString();
		}
		return result;
	}
	
	public String getEscapedClientId()
	{
		String clientId = getClientId();
		if(clientId!=null)
		{
			return clientId.replaceAll(":", "_");
		}
		return clientId;
	}

}
